from bs4 import BeautifulSoup
import urllib
import urllib.request
from lxml import etree
import os

from zc_getdata.toSave import save_Single_Project
from zc_getdata.getProjectData import BASE_URL
from zc_getdata.toSave import saveList
from zc_getdata.openurl import openurl

url = 'http://www.taskcity.com/projects'


# 从单个project_page 解析信息
def getproject_info(htmlname) -> dict:
    parser = etree.HTMLParser(encoding='utf-8')
    tree = etree.parse(htmlname, parser=parser)
    p = {}
    p['pid'] = tree.xpath('//div[@class="panel-body"]/div[@class="list"]/div[1]/span/text()')[0]
    p['state'] = tree.xpath('//div[@class="panel-body"]/div[@class="list"]/div[2]/span/text()')[0]
    p['cost'] = tree.xpath('//div[@class="panel-body"]/div[@class="list"]/div[3]/span/text()')[0]
    p['timecost'] = tree.xpath('//div[@class="panel-body"]/div[@class="list"]/div[4]/span/text()')[0]
    # 发包方
    p['owner'] = ''
    # 接包方 使用set去重
    contractor_name = tree.xpath('//table[@class ="bid_table "]//td/span[@id="win_bid"]/../a[2]/text()')
    contractor_url = tree.xpath('//table[@class ="bid_table "]//td/span[@id="win_bid"]/../a[2]//@href')
    if len(contractor_name) != 0:
        contractor_name = contractor_name[0]
        contractor_url = contractor_url[0]
    contractor = contractor_name, contractor_url
    p['contractor_name'] = contractor_name
    p['contractor_url'] = contractor_url
    # 项目名称
    p['pname'] = tree.xpath('//a[@class="tag-black"][2]/text()')[0]
    # 发布日期
    p['date'] = tree.xpath('//div[@class="panel-body"]/div[@class="list"]/div[5]/span/text()')[0]
    # 项目描述
    p['description'] = ''
    # 抢包列表bids，使用set去重
    bidders_name = tree.xpath('//table[@class ="bid_table "]//td/a[2]//text()')
    bidders_url = tree.xpath('//table[@class ="bid_table "]//td/a[2]/@href')
    bids = []
    for i in range(len(bidders_name)):
        bid = bidders_name[i], BASE_URL + bidders_url[i]
        bids.append(bid)
    p['bids'] = set(bids)
    return p


def get_AllProjectInfo():
    htmlpath = './data/projectHtml/'

    def get_imlist(path):
        return [os.path.join(htmlpath, f) for f in os.listdir(path) if f.endswith('.html')]

    project_page_list = get_imlist(htmlpath)
    l = []
    for pro_page in project_page_list:
        l.append(getproject_info(pro_page))
        p = getproject_info(pro_page)
        save_Single_Project(p)
    return l


if __name__ == '__main__':
    # get_Project_Html()
    get_AllProjectInfo()
    # p = getproject_info(htmlname='./data/projectHtml/p_231243.html')
    # save_Single_Project(p)
